<template>
  <div class="login-wrap">
    <div class="ms-login">
      <div class="ms-title">后台管理系统</div>
      <el-form :model="userInfo" :rules="rules" ref="login" label-width="0px" class="ms-content">
        <el-form-item prop="code">
          <el-input v-model="userInfo.code" placeholder="学号">
            <template #prepend>
              <el-button :icon="User"></el-button>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="userInfo.password" placeholder="密码">
            <template #prepend>
              <el-button :icon="Lock"></el-button>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-input v-model="userInfo.name" placeholder="姓名">
            <template #prepend>
              <el-button :icon="Tickets"></el-button>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-input v-model="userInfo.card" placeholder="身份证号">
            <template #prepend>
              <el-button :icon="Postcard"></el-button>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-input v-model="userInfo.mobile" placeholder="电话号码">
            <template #prepend>
              <el-button :icon="Phone"></el-button>
            </template>
          </el-input>
        </el-form-item>
<!--        <el-form-item prop="password">-->
<!--          <el-input-->
<!--              type="password"-->
<!--              placeholder="password"-->
<!--              v-model="param.password"-->
<!--              @keyup.enter="submitForm(login)"-->
<!--          >-->
<!--            <template #prepend>-->
<!--              <el-button :icon="Lock"></el-button>-->
<!--            </template>-->
<!--          </el-input>-->
<!--        </el-form-item>-->
        <div class="login-btn">
          <el-button type="primary" @click="submitForm(login)">注册</el-button>
        </div>
        <p class="login-tips" @click="toLogin">前往登录</p>
      </el-form>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue';
import { useTagsStore } from '../../store/tags';
import { usePermissStore } from '../../store/permiss';
import { useRouter } from 'vue-router';
import { ElMessage } from 'element-plus';
import type { FormInstance, FormRules } from 'element-plus';
import {Lock, Phone, Postcard, Tickets, User} from '@element-plus/icons-vue';
import {subLogin} from '@/api/user'
import {add} from "@/api/student";

interface LoginInfo {
  code: string,
  password: string,
  name: string,
  sex: string,
  entryYear:number,
  card: string,
  mobile: string,
  profession:string,
  classGrade: string,
}

const router = useRouter();
const userInfo = reactive<LoginInfo>({
  code: '',
  password: '',
  name: '',
  sex: '',
  entryYear: 2024,
  card: '',
  mobile: '',
  profession: '',
  classGrade: '',
});

const rules: FormRules = {
  code: [
    {
      required: true,
      message: '请输入学号',
      trigger: 'blur'
    }
  ],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
};
const permiss = usePermissStore();
const login = ref<FormInstance>();
const submitForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  formEl.validate((valid: boolean) => {
    if (valid) {

      add(userInfo).then(res=>{
        ElMessage.success("注册成功，请前往登录")
      })
      //
      // ElMessage.success('登录成功');
      // localStorage.setItem('ms_username', param.username);
      // const keys = permiss.defaultList[param.username == 'admin' ? 'admin' : 'user'];
      // permiss.handleSet(keys);
      // localStorage.setItem('ms_keys', JSON.stringify(keys));
      // router.push('/');
    } else {
      ElMessage.error('登录成功');
      return false;
    }
  });
};
const toLogin = () =>{
  console.log("点击")
  router.push('/login');
}
const tags = useTagsStore();
tags.clearTags();
</script>

<style scoped>
.login-wrap {
  position: relative;
  width: 100%;
  height: 100%;
  background-image: url(../../assets/img/login-bg.jpg);
  background-size: 100%;
}
.ms-title {
  width: 100%;
  line-height: 50px;
  text-align: center;
  font-size: 20px;
  color: #fff;
  border-bottom: 1px solid #ddd;
}
.ms-login {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 350px;
  margin: -190px 0 0 -175px;
  border-radius: 5px;
  background: rgba(255, 255, 255, 0.3);
  overflow: hidden;
}
.ms-content {
  padding: 30px 30px;
}
.login-btn {
  text-align: center;
}
.login-btn button {
  width: 100%;
  height: 36px;
  margin-bottom: 10px;
}
.login-tips {
  font-size: 12px;
  line-height: 30px;
  color: #069de8;
}
</style>
